const app = getApp();
const db = wx.cloud.database()
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    index: null,
    // 上传头像数
    uphead: 0,
    avatar: '',
    imgList: [],
    imgs: [],
    modalName: null,
    textareaAValue: '',
    textareaBValue: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.authentication();
  },
  PickerChange(e) {
    console.log(e);
    this.setData({
      index: e.detail.value
    })
  },

  TimeChange(e) {
    this.setData({
      time: e.detail.value
    })
  },
  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  RegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  // 上传头像
  headImage() {
    let that = this;
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        wx.showLoading({
          title: '上传中',
          mask: true
        })
        var now = new Date()
        wx.cloud.uploadFile({
          cloudPath: "idol" + '/' + "avatar" + '/' + now.getTime() + '.png',
          filePath: res.tempFilePaths[0],
          success: res2 => {
            console.log(res2)
            that.setData({
              uphead: 1,
              avatar: res2.fileID
            })
          },
          fail: err => {

          }
        })
        wx.showToast({
          title: '上传成功 ',
          icon: 'none'
        })
      }
    });
  },
  delHead(e) {
    wx.showModal({
      title: '删除确认',
      content: '确定要删除这张照片吗？',
      cancelText: '取消',
      confirmText: '删除',
      success: res => {
        if (res.confirm) {
          var files = new Array(1);
          files[0] = this.data.avatar;
          this._removeFile(files);
          this.setData({
            uphead: 0,
            avatar: ''
          })
        }
      }
    })
  },

  _removeFile(files){
    wx.cloud.deleteFile({
      fileList:files,
      success(res){
        console.log(res,'删除文件')
      },
      fail(err){
        console.log(err)
      }
    })
  },
  ChooseImage() {
    let imgs = this.data.imgs;
    wx.chooseImage({
      count: 4, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {

        wx.showLoading({
          title: '上传中',
          mask: true
        })
        var suc = 0;
        var fail = 0;
        var tl = res.tempFilePaths.length;
        if (tl != 0) {
          var b = new Array(tl);
          console.log("234234=" + tl)
          for (var i in res.tempFilePaths) {
            var now = new Date()
            wx.cloud.uploadFile({
              cloudPath: "idol" + '/' + "photo" + '/' + now.getTime() + '_' + i + '.png',
              filePath: res.tempFilePaths[i],
              success: res2 => {
                suc++
                imgs.push(res2.fileID);
                // b[i] = res.fileID;//此处异步如何处理？？？？？？？
                that.setData({
                  logFormPic: res2.fileID
                })
              },
              fail: err => {
                fail++;
              }
            })
          }
          this.setData({
            imgs: imgs,
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          // console.log("3333333333")
          this.setData({
            imgList: res.tempFilePaths
          })
        }
        wx.showToast({
          title: '上传成功！',
          icon: 'none'
        })
        // 、、、、、、、、、、、、、、、、、、、、、
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '删除确认',
      content: '确定要删除这张照片吗？',
      cancelText: '取消',
      confirmText: '删除',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.data.imgs.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList,
            imgs: this.data.imgs
          })
        }
      }
    })
  },
  textareaAInput(e) {
    this.setData({
      textareaAValue: e.detail.value
    })
  },
  textareaBInput(e) {
    this.setData({
      textareaBValue: e.detail.value
    })
  },

  //删除图片
  delPic: function () {
    let that = this,
      data = that.data,
      logFormPic = data.logFormPic
    if (logFormPic.length > 0) {
      wx.showModal({
        title: '删除提示',
        content: '确定要删除图片？',
        success: function (res) {
          if (res.confirm) {
            that.setData({
              logFormPic: ''
            })
          }
        }
      })
    }
  },
  //提交表单
  saveIdol: function (e) {
    console.log("saveIdol");
    let that = this,
      data = that.data,
      avatar = data.avatar,
      imgs = data.imgs,
      name = e.detail.value.name,
      detail = e.detail.value.detail

    if (name.length == 0) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
    } else {
      wx.showLoading({
        title: '加载中',
        mask: true
      })

      let time = app.nowTimeStamp();

      db.collection('ad_idol').add({
        data: {
          name: name,
          detail: detail,
          avatar: avatar,
          photos: imgs,
          fans: 0,
          votes: 0,
          hasdel: 0,
          create_time: time,
          update_time: time
        },
        success: res => {
          wx.hideLoading()
          // wx.showToast({
          //   title: '添加成功',
          //   icon: 'success'
          // })
          that.showModal('suc');
        },
        fail: err => {
          wx.hideLoading()
          wx.showToast({
            title: '添加失败',
            icon: 'none'
          })
        }
      })

    }
  },
  // showModal(e) {
  //   this.setData({
  //     modalName: e.currentTarget.dataset.target
  //   })
  // },
  //打开
  urlTap: function (e) {
    this.hideModal();
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
  },
  goUrl: function (url) {
    // '/pages/idol/detail?id=' + id
    wx.navigateTo({
      url: url
    })
  },
  showModal(modal) {
    this.setData({
      modalName: modal
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  _goBack: function () {
    wx.navigateBack({
      delta: 1
    });
  },
  _goHome: function () {
    wx.navigateTo({
      url: '/pages/my/admin/idol/add',
    });
  }

})